package com.snowflakes.gruntr;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.parse.ParseFile;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseRelation;
import com.parse.ParseUser;

public class CustomAdapter extends ParseQueryAdapter<ParseObject> {

	
	public CustomAdapter(Context context) {
		// Use the QueryFactory to construct a PQA that will only show
		// Todos marked as high-pri
		super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
			public ParseQuery create() {
				
				ParseUser user = ParseUser.getCurrentUser();
				
				ParseRelation<ParseUser> relation = user.getRelation("friends");
		        ParseQuery friendsQuery = relation.getQuery();
				
				ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Post");
				query.whereMatchesQuery("CreatedBy", friendsQuery);
				query.addDescendingOrder("createdAt");
				return query;
			}
		});
	}

	// Customize the layout by overriding getItemView
	@Override
	public View getItemView(ParseObject object, View v, ViewGroup parent) {
		if (v == null) {
			v = View.inflate(getContext(), R.layout.individual_post, null);
		}

		super.getItemView(object, v, parent);

		// Add and download the image
		ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.postImage);
		ParseFile imageFile = object.getParseFile("Image");
		if (imageFile != null) {
			todoImage.setParseFile(imageFile);
			todoImage.loadInBackground();
		}

		// Add the title view
		TextView titleTextView = (TextView) v.findViewById(R.id.text1);
		titleTextView.setText(object.get("Name").toString());

		// Add a reminder of how long this item has been outstanding
		TextView timestampView = (TextView) v.findViewById(R.id.timestamp);
		timestampView.setText(object.getCreatedAt().toString());
		
		TextView gruntCount = (TextView) v.findViewById(R.id.showGrunts);
		gruntCount.setText("Show Grunts (" + object.getNumber("Grunts") + ")");
		gruntCount.setTag(object.getObjectId());
		
		Button gruntAtIt = (Button) v.findViewById(R.id.buttonthang);
		gruntAtIt.setTag(object.getObjectId());
		
		
		return v;
	}
	
	

}

